﻿var actionUrl = 'ashx/mobilehandler.ashx';
var markerClusterer;
var markers = [];
var pt;
var uid = "";
var map;
var data_info;
var opts;
var keyid = "";

function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}
$(function () {
    //did = getUrlParam('did');
    var size = { width: $(window).width(), height: $(window).height() };
    mylayout.init(size);
    opts = {
        width: 250,     // 信息窗口宽度
        height: 80,     // 信息窗口高度
        title: "测报点信息", // 信息窗口标题
        enableMessage: false//设置允许信息窗发送短息
    };
    InitMap();
    autoResize({ dataGrid: '#mGrid', gridType: 'datagrid', callback: mygrid.bindGrid, height:40 });
    $(window).resize(function () {
        size = { width: $(window).width(), height: $(window).height() };
        mylayout.resize(size);
    });

});
var mylayout = {
    init: function (size) {
        $('#layout').width(size.width - 4).height(size.height - 4).layout();
        var center = $('#layout').layout('panel', 'center');
        center.panel({
            onResize: function (w, h) {
                $('#mGrid').datagrid('resize', { width: w - 6, height: h - 36 });
            }
        });
    },
    resize: function (size) {
        mylayout.init(size);
        $('#layout').layout('resize');
    }
};
var mygrid = {
    bindGrid: function (size) {
        $('#mGrid').datagrid({
            toolbar: '#toolbar',
            //title: '上传列表',
            iconCls: 'icon icon-list',
            width: 300,
            height: size.height,
            nowrap: false,
            rownumbers: true,
            animate: true,
            url: actionUrl,
            singleSelect:true,
            columns: [[
                //{ title: 'mobileSendInfoId', field: 'mobileSendInfoId', width: 100 },
				{ title: '发送人', field: 'sendUid', width: 50 },
				//{ title: 'receiveId', field: 'receiveId', width: 50 },
				//{ title: 'msphoto', field: 'msphoto', width: 100 },
				{ title: 'sendTime', field: 'sendTime', width: 180 }

            ]],
            onClickRow: function (rowIndex, rowData) {
                var ggPoint = new BMap.Point(rowData.GPSLongitude, rowData.GPSLatitude);
                keyid = rowData.KeyId;
                setTimeout(function () {                    
                    BMap.Convertor.translate(ggPoint, 0, translateCallback);     //GCJ-02坐标转成百度坐标
                }, 1000);                
            },
            onLoadSuccess: function (data) {
                //data_info = [];
                //map.clearOverlays();
                //$.each(data.rows, function (k, v) {
                //    data_info.push({ "KeyId": v["KeyId"], "lng": v["GPSLongitude"], "lat": v["GPSLatitude"] });

                //});
                //setTimeout(function () {
                //    BMap.Convertor.transMore(points,0, callback);        //一秒之后开始进行坐标转换。参数2，表示是从GCJ-02坐标到百度坐标。参数0，表示是从GPS到百度坐标
                //}, 1000);
                //var marker;

                //pt = "";
                //if (markers.length > 0) {
                //    markerClusterer.removeMarkers(markers);
                //    markers = [];
                //}

                //for (var i = 0; i < data_info.length; i++) {                   
                //    pt = new BMap.Point(data_info[i].lng, data_info[i].lat);                    
                //    marker = new BMap.Marker(pt);
                //    var content = "<img style='float:right;margin:4px' id='imgDemo' src='" +actionUrl+"?action=pic&KeyId="+data_info[i].KeyId + "' width='139' height='104' />" ;
                //    map.addOverlay(marker);
                //    markers.push(marker);
                //    addClickHandler(content, marker);
                //}
                //markerClusterer = new BMapLib.MarkerClusterer(map, { markers: markers });//对地图扩大缩小后会都所有的点全显示出来。
                
            }
        });
        $('#mGrid').datagrid('getPager').pagination({
            layout: ['list', 'prev', 'next', 'refresh']
        });
    },
    reload: function () {
        $('#mGrid').datagrid('clearSelections').datagrid('reload');
    },
    selectRow: function () {
        return $('#mGrid').datagrid('getSelected');
    }
}

var crud = {
    createParam: function (action, keyid) {
        var o = {};
        var query = top.$('#myForm').serializeArray();
        query = convertArray(query);
        o.jsonEntity = JSON.stringify(query);
        o.action = action;
        o.keyid = keyid;
        return "json=" + JSON.stringify(o);
    }
}
function InitMap() {
    map = new BMap.Map("mymap");          // 创建地图实例 
    map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
    map.enableScrollWheelZoom(true);
    var top_left_control = new BMap.ScaleControl({ anchor: BMAP_ANCHOR_TOP_LEFT });// 左上角，添加比例尺
    var top_left_navigation = new BMap.NavigationControl();  //左上角，添加默认缩放平移控件
    map.addControl(top_left_control);
    map.addControl(top_left_navigation);
   map.centerAndZoom("聊城", 11);
}


function addClickHandler(content, marker) {
    marker.addEventListener("click", function (e) {
        openInfo(content, e);

    }
    );
}
function openInfo(content, e) {
    var p = e.target;
    var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
    //var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象 
    var infoWindow = new BMap.InfoWindow(content);
    map.openInfoWindow(infoWindow, point); //开启信息窗口
    //document.getElementById('imgDemo').onload = function () {
    //    //infoWindow.redraw();   //防止在网速较慢，图片未加载时，生成的信息框高度比图片的总高度小，导致图片部分被隐藏
    //}
}
function callback(xyResults) {
    var xyResult = null;
    for (var index in xyResults) {
        xyResult = xyResults[index];
        if (xyResult.error != 0) { continue; }//出错就直接返回;
        var point = new BMap.Point(xyResult.x, xyResult.y);
        var marker = new BMap.Marker(point);
        map.addOverlay(marker);
        map.setCenter(point);// 由于写了这句，每一个被设置的点都是中心点的过程
    }
}
translateCallback = function (point) {
    var marker = new BMap.Marker(point);
    var content = "<img style='margin:4px' id='imgDemo' src='" + actionUrl + "?action=pic&KeyId=" + keyid + "' width='300' height='200' />";
    map.addOverlay(marker);       
    addClickHandler(content, marker);
    map.setCenter(point);
    
}


